<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function index()
    {
        $class    = $this->class_comment();
        $goods    = M('goods')->select();
        $property = M('property')->select();
        foreach ($goods as &$v) {
            foreach ($property as &$vv) {
                if ($v['id'] == $vv['gid']) {
                    $v['shuxing'][] = $vv;
                }
            }
        }
        $classify   = $this->digui(0);
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('class', $class);
        $this->assign('goods', $goods);
        $this->display();
    }

    public function goods()
    {
        $property = D('property');
        $goods    = M('goods')->where(array('id' => I('get.id')))->find();
        $res      = $property->where(array('gid' => $goods['id']))->select();
        $pic      = array();
        $count    = '';
        foreach ($res as &$v) {
            $pic[] = explode(',', $v['pic']);
            $count[] .= $v['count'];
        }
        foreach ($count as $add) {
            $counts += $add;
        }
        $cate       = $this->get_parent_category($goods['cid']);
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('count', $counts);
        $this->assign('pic', $pic);
        $this->assign('goods', $goods);
        $this->assign('cate', $cate);
        $this->assign('res', $res);
        $this->display();
    }

    public function flow_add()
    {
        $flow = D('flow');
        $res  = $flow->create(I('post.'));
        if ($res) {
            $add = $flow->add();
            if ($add) {
                $this->success('添加购物车成功');
            } else {
                $this->error($flow->getError());
            }
        } else {
            $this->error($flow->getError());
        }
    }

    public function goods_size()
    {
        $res = D("property")->where(array('id' => I('post.id')))->find();
        // foreach($res as &$v){
        //     $v['pic'] = explode(',',$v['pic']);
        // }
        if ($res) {
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = $res;
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = null;
        }
        $this->ajaxReturn($return);
    }

    public function lists()
    {
        $fid      = I('get.fid');
        $good     = D('goods');
        $classify = $this->digui(0);
        $arr      = $this->list_goods($fid);
        if ($arr == null) {
            $arr = D('classify')->where(array('id' => $fid))->find()['id'];
        }
        $goods = $good->where(array('cid' => array('in', $arr)))->select();
        $res   = array_filter($goods);
        $pic   = M('property')->select();
        foreach ($res as &$r) {
            $r['pic'] = M('property')->where(array('gid' => $r['id']))->find()[pic];
        }
        $good       = D('goods')->field('onethink_goods.*,onethink_property.pic')->join('onethink_property on onethink_property.gid=onethink_goods.id')->limit('1,5')->select();
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('good', $good);
        $this->assign('classify', $classify);
        $this->assign('res', $res);
        $this->display();
    }

    public function list_goods($id, $arr = array())
    {
        $res = D('classify')->where(array('fid' => $id))->select();
        if ($res) {
            foreach ($res as $v) {
                $arr[] = $v['id'];
                $arr   = $this->list_goods($v['id'], $arr, $lv);
            }
        }
        return ($arr);
    }

    public function digui($fid, $arr = array(), $lv)
    {
        $lv++;
        $res = D('classify')->where(array('fid' => $fid))->select();
        if ($res) {
            foreach ($res as $v) {
                $v['lv'] = $lv;
                $arr[]   = $v;
                $arr     = $this->digui($v['id'], $arr, $lv);
            }
        }
        return $arr;
    }

    public function news()
    {
        $this->display();
    }

    public function favorite()
    {
        $res = M('collect')->field('onethink_collect.*,onethink_property.gid,onethink_property.cost,onethink_property.pic,onethink_property.count')->join('onethink_property on onethink_collect.pid = onethink_property.id')->where(array('uid' => session('id')))->select();
        foreach ($res as &$v) {
            $v['pic']     = substr($v['pic'], 0, 28);
            $v['content'] = M('goods')->where(array('id' => $v['gid']))->select()[0]['content'];
        }
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('res', $res);
        $this->display();
    }

    public function favorite_delete()
    {
        $res = M('collect')->where(array('id' => I('get.id')))->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error(M('collect')->getError());
        }
    }

    public function favorite_ajax()
    {
        echo '取消关注';
    }

    public function error()
    {
        $this->display();
    }

    public function comment()
    {
        $this->display();
    }

    public function flow()
    {
        $flow = D('flow');
        $res  = $flow->field('onethink_flow.*,onethink_property.gid')->join('onethink_property on onethink_flow.pid = onethink_property.id')->where(array('onethink_flow.uid' => session('id'), 'onethink_flow.active' => 0))->select();

        $goods = D('goods');
        $sum   = 0;
        foreach ($res as &$v) {
            $v['content'] = $goods->where(array('id' => $v['gid']))->select()[0]['content'];
            $v['costs']   = $v['num'] * $v['cost'];
            $sum += $v['costs'];
        }
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('sum', $sum);
        $this->assign('res', $res);
        $this->display();
    }

    public function flow_ajax()
    {
        $flow = D('flow');
        $res  = $flow->create();
        if ($res) {
            $res1 = $flow->where(array('id' => I('post.id')))->setField('num', I('post.num'));
            if ($res1) {
                $this->success('修改成功');
            } else {
                $this->error($flow->getError());
            }
        } else {
            $this->error($flow->getError());
        }
    }
    public function flow_delete()
    {
        $id   = I('post.id');
        $flow = D('flow');
        $res  = $flow->where(array('id' => $id))->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error($flow->getError());
        }
    }

    public function collect_add()
    {
        $_POST['uid'] = session('id');
        $collect      = D('collect');
        $res          = $collect->create();
        if ($res) {
            if ($collect->add()) {
                $this->success('收藏成功');
            } else {
                $this->error($collect->getError());
            }
        } else {
            $this->error($collect->getError());
        }
    }

    public function flow2()
    {
        $res = M('address')->where(array('uid' => session('id')))->select();
        foreach ($res as &$v) {
            $v['address1'] = M('placename')->where(array('id' => $v['address1']))->select()[0]['name'];
            $v['address2'] = M('placename')->where(array('id' => $v['address2']))->select()[0]['name'];
            $v['address3'] = M('placename')->where(array('id' => $v['address3']))->select()[0]['name'];
        }
        $placename = M('placename')->where(array('fid' => 0))->select();
        $this->assign('res', $res);
        $this->assign('placename', $placename);
        $this->display();
    }

    public function flow2_address()
    {
        $_POST['uid'] = session('id');
        $address      = D('address');
        $res          = $address->create(I('post.'));
        if ($res) {
            if ($address->add()) {
                $this->success('添加成功');
            } else {
                $this->error($address->getError());
            }
        } else {
            $this->error($address->getError());
        }
    }

    public function order_success1()
    {
        if (!I('get.id')) {
            $this->success('请先选择收货地址', U('flow2'));
            die;
        }
        $flow                = D('flow');
        $property            = D('property');
        $placename           = D('placename');
        $order               = D('order');
        $address             = D('address')->where(array('id' => I('get.id')))->find();
        $address['address1'] = $placename->where(array('id' => $address['address1']))->find()['name'];
        $address['address2'] = $placename->where(array('id' => $address['address2']))->find()['name'];
        $address['address3'] = $placename->where(array('id' => $address['address3']))->find()['name'];
        $dizhi               = $address['address1'] . $address['address2'] . $address['address3'] . $address['address'];
        $tel                 = $address['phone'];
        $res                 = $flow->select();
        foreach ($res as &$v) {
            $v['orderId']     = time() . session('id') . $v['id'];
            $color            = $property->where(array('id' => $v['pid']))->find()['color'];
            $size             = $property->where(array('id' => $v['pid']))->find()['size'];
            $v['content']     = $color . $size;
            $v['address']     = $dizhi;
            $v['costs']       = $v['num'] * $v['cost'];
            $v['name']        = $address['name'];
            $v['postalcode']  = $address['postalcode'];
            $v['phone']       = $address['phone'];
            $v['mobel_phone'] = $address['mobel_phone'];
            $v['id']          = '';
        }
        $addAll = $order->addAll($res);
        if($addAll){
            $this->success('添加订单成功',U('order_success'));
        }
        $this->assign('res', $res);
    }

    public function order_success(){
        $res = D('order')->where(array('uid'=>session('id')))->select();
        foreach($res as &$v){
            $v['costs'] = $v['num']*$v['cost'];
            $zongjia += $v['costs'];
        }
        $this->assign('zongjia',$zongjia);
        $this->assign('res',$res);
        $this->display();
    }

    public function pay()
    {
        $this->display();
    }

    public function advertisement()
    {
        $res = M('advertisement')->select();
        $this->assign('res', $res);
        $this->display();
    }

    public function get_parent_category($cid)
    {
        if (empty($cid)) {
            return false;
        }
        $cates = M('classify')->where(array('active' => 1))->field('id,title,fid')->select();
        $child = $this->get_category($cid); //获取参数分类的信息
        $fid   = $child['fid'];
        $temp  = array();
        $res[] = $child;
        while (true) {
            foreach ($cates as $key => $cate) {
                if ($cate['id'] == $fid) {
                    $fid = $cate['fid'];
                    array_unshift($res, $cate); //将父分类插入到数组第一个元素前
                }
            }
            if ($fid == 0) {
                break;
            }
        }
        return $res;
    }

    public static function get_category($id, $field = null)
    {
        static $list;

        /* 非法分类ID */
        if (empty($id) || !is_numeric($id)) {
            return '';
        }

        /* 读取缓存数据 */
        if (empty($list)) {
            $list = S('sys_category_list');
        }

        /* 获取分类名称 */
        if (!isset($list[$id])) {
            $cate = M('classify')->find($id);
            if (!$cate || 1 != $cate['active']) {
                //不存在分类，或分类被禁用
                return '';
            }
            $list[$id] = $cate;
            S('sys_category_list', $list); //更新缓存
        }
        return is_null($field) ? $list[$id] : $list[$id][$field];
    }

    public function class_comment()
    {
        $classify  = M('classify');
        $classify1 = $classify->where(array('fid' => 0))->select();
        foreach ($classify1 as &$c) {
            $c['class'] = $classify->where(array('fid' => $c['id']))->select();
            foreach ($c['class'] as &$cl) {
                $cl['class'] = $classify->where(array('fid' => $cl['id']))->select();
            }
        }
        return $classify1;
    }
}
